package com.leetcode.stack_queue;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author Dennis Li
 * @date 2020/7/24 20:23
 */
public class BracketValidation_20 {


    public boolean isValid(String s) {
        final Map<Character, Character> map = new HashMap<>();
        map.put(')', '(');
        map.put('}', '{');
        map.put(']', '[');
        if (s.equals("")) return true;
        final Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (stack.isEmpty())
                stack.push(ch);
            else if (stack.peek().equals(map.get(ch)))
                stack.pop();
            else if (map.containsKey(ch))
                return false;
            else
                stack.push(ch);
        }
        return stack.isEmpty();
    }

}
